# coding=utf-8
"""
author: jamon
"""
import sys
from typing import Optional, List

import pygame
from pygame.event import Event
from pygame.font import Font
from pygame.surface import Surface

from client.config import ClientConfig
from client.views.base import ScreenBase, register_screen
from client.views.scene_mgr import scene_mgr
from obespoir.share.espoir_log import global_log as logger

color_white = (255, 255, 255)
color_red = (255, 0, 0)
_need_reload = True


def switch_other_screen():
    new_screen = "battle"
    logger.info("start", new_screen=new_screen)
    gate_id = 1
    scene_mgr.switch_to_scene(new_screen, gate_id=gate_id)


@register_screen("game_over")
class GameOverScreen(ScreenBase):

    def __init__(self, screen: Surface):
        super(GameOverScreen, self).__init__(screen=screen)
        self.config = ClientConfig()
        self.background_img: Surface = pygame.image.load(self.config.background_image_path)

        width = self._screen.get_width()
        height = self._screen.get_height()
        self.font: Font = pygame.font.SysFont("Arial", width // 12)

        # 游戏失败图
        self.gameover_img = pygame.image.load(self.config.game_over)
        self.gameover_img = pygame.transform.scale(self.gameover_img, (150, 75))
        self.gameover_img_rect = self.gameover_img.get_rect()
        self.gameover_img_rect.midtop = width / 2, height / 8
        self.gameover_flash_time = 25
        self.gameover_flash_count = 0
        self.gameover_show_flag = True

        # 用于选择退出或重新开始
        self.tank_cursor = pygame.image.load(self.config.tank_cursor_image)\
            .convert_alpha().subsurface((0, 144), (48, 48))
        self.tank_rect = self.tank_cursor.get_rect()
        self.restart_render_white = self.font.render('RESTART', True, color_white)
        self.restart_render_red = self.font.render('RESTART', True, color_red)
        self.restart_rect = self.restart_render_white.get_rect()
        self.restart_rect.left, self.restart_rect.top = width / 2.4, height / 2
        self.quit_render_white = self.font.render('QUIT', True, color_white)
        self.quit_render_red = self.font.render('QUIT', True, color_red)
        self.quit_rect = self.quit_render_white.get_rect()
        self.quit_rect.left, self.quit_rect.top = width / 2.4, height / 1.6
        self.is_quit_game = False

        self.font_render: Optional[Surface] = None
        self.font_rect: Optional[pygame.Rect] = None

        self.is_win: bool = False

    def init_screen(self, is_win: bool, *args, **kwargs):
        width = self._screen.get_width()
        height = self._screen.get_height()
        # 游戏胜利与否的提示
        if is_win:
            self.font_render = self.font.render('Congratulations, You win!', True, color_white)
        else:
            self.font_render = self.font.render('Sorry, You fail!', True, color_white)
        self.font_rect = self.font_render.get_rect()
        self.font_rect.centerx, self.font_rect.centery = width / 2, height / 3

    async def update(self, events: List[Event] = None, **kwargs):
        is_selected = False
        for event in events:
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RETURN:
                    is_selected = True
                elif event.key == pygame.K_UP or event.key == pygame.K_DOWN or \
                        event.key == pygame.K_w or event.key == pygame.K_s:
                    self.is_quit_game = not self.is_quit_game
        if not is_selected:
            self._screen.blit(self.background_img, (0, 0))
            self.gameover_flash_count += 1
            if self.gameover_flash_count > self.gameover_flash_time:
                self.gameover_show_flag = not self.gameover_show_flag
                self.gameover_flash_count = 0
            if self.gameover_show_flag:
                self._screen.blit(self.gameover_img, self.gameover_img_rect)
            self._screen.blit(self.font_render, self.font_rect)
            if not self.is_quit_game:
                self.tank_rect.right, self.tank_rect.top = self.restart_rect.left - 10, self.restart_rect.top
                self._screen.blit(self.restart_render_red, self.restart_rect)
                self._screen.blit(self.quit_render_white, self.quit_rect)
            else:
                self.tank_rect.right, self.tank_rect.top = self.quit_rect.left - 10, self.quit_rect.top
                self._screen.blit(self.restart_render_white, self.restart_rect)
                self._screen.blit(self.quit_render_red, self.quit_rect)
            self._screen.blit(self.tank_cursor, self.tank_rect)
        else:
            if self.is_quit_game:
                pygame.quit()
                sys.exit()
            else:
                switch_other_screen()
